package com.club.football.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.football.common.UserHolder;
import com.club.football.dto.FeedBackStarDto;
import com.club.football.dto.FeedbackEditDto;
import com.club.football.dto.FeedbackSaveDto;
import com.club.football.dto.PageDto;
import com.club.football.entity.Feedback;
import com.club.football.entity.Staff;
import com.club.football.mapper.FeedbackMapper;
import com.club.football.req.FeedbackPageReq;
import com.club.football.service.FeedbackService;
import com.club.football.service.StaffService;
import com.club.football.utils.BeanUtils;
import com.club.football.utils.R;
import com.club.football.vo.FeedbackVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.club.football.common.CommonConstants.*;
import static com.club.football.common.enums.FeedBackStatusEnum.PENDING;

/**
 * 实施反馈服务
 *
 * @author 1
 * @date 2024/01/09
 */
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {
    @Resource
    private StaffService staffService;


    @Override
    public R<PageDto<Feedback>> feedbackPage(PageDto<Feedback> pageDto, FeedbackPageReq feedbackPageReq) {
        lambdaQuery().like(StringUtils.isNotBlank(feedbackPageReq.getFeedbackTitle()), Feedback::getFeedbackTitle, feedbackPageReq.getFeedbackTitle())
                .eq(StringUtils.isNotBlank(feedbackPageReq.getType()), Feedback::getType, feedbackPageReq.getType())
                .eq(StringUtils.isNotBlank(feedbackPageReq.getPhone()), Feedback::getPhone, feedbackPageReq.getPhone())
                .eq(StringUtils.isNotBlank(feedbackPageReq.getStatus()), Feedback::getStatus, feedbackPageReq.getStatus())
                .page(pageDto);
        return R.ok(pageDto, GET_INFO_OK);
    }

    @Override
    public R<String> feedbackAdd(FeedbackSaveDto saveDto) {
        Feedback feedback = BeanUtils.copier(saveDto, new Feedback());
        feedback.setType(saveDto.getType().getType());
        feedback.setStatus(PENDING.getStatus());
        long id = IdWorker.getId();
        feedback.setFeedbackId(id);
        save(feedback);
        return R.ok(String.valueOf(id), SAVE_OK_MSG);
    }

    /**
     * 反馈编辑
     *
     * @param saveDto 将数据保存到
     * @return {@link R}<{@link String}>
     */
    @Override
    public R<String> feedbackEdit(FeedbackEditDto saveDto) {
        Feedback feedback = BeanUtils.copier(saveDto, new Feedback());
        Long userId = UserHolder.getUser().getUserId();
        feedback.setUpdateBy(userId);
        if (feedback.getResolverId() != null) {
            Staff staff = staffService.getById(feedback.getResolverId());
            feedback.setResolver(staff.getName());
        }
        updateById(feedback);
        return R.ok(EDIT_OK_MSG);
    }

    /**
     * Set Star
     *
     * @param feedBackStarDto 反馈星形dto
     * @return {@link R}<{@link String}>
     */
    @Override
    public R<String> setStar(FeedBackStarDto feedBackStarDto) {
        Feedback feedback = getById(feedBackStarDto.getFeedbackId());
        if (feedback == null || !feedBackStarDto.getPhone().equals(feedback.getPhone())) {
            return R.failed("反馈" + NO_EXIST_MSG);
        }
        if (PENDING.getStatus().equals(feedback.getStatus())) {
            return R.failed("反馈未开始处理");
        }
        lambdaUpdate().eq(Feedback::getFeedbackId, feedBackStarDto.getFeedbackId())
                .set(Feedback::getStar, feedBackStarDto.getStar()).update();
        return R.ok("评分成功，感谢您的支持与理解！");
    }

    @Override
    public R<FeedbackVo> feedbackInfo(FeedBackStarDto feedBackStarDto) {
        Feedback feedback = getById(feedBackStarDto.getFeedbackId());
        if (feedback == null || !feedBackStarDto.getPhone().equals(feedback.getPhone())) {
            return R.failed("反馈" + NO_EXIST_MSG);
        }
        FeedbackVo feedbackVo = BeanUtils.copier(feedback, new FeedbackVo());
        return R.ok(feedbackVo, GET_INFO_OK);
    }
}
